﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;

namespace l1cs.server.serverpackets
{
    class S_DoorPack : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_DoorPack ).FullName );

        private const int STATUS_POISON = 1;
        private const int STATUS_INVISIBLE = 2;
        private const int STATUS_PC = 4;
        private const int STATUS_FREEZE = 8;
        private const int STATUS_BRAVE = 16;
        private const int STATUS_ELFBRAVE = 32;
        private const int STATUS_FASTMOVABLE = 64;
        private const int STATUS_GHOST = 128;

        private byte[] _byte = null;

        public S_DoorPack( L1DoorInstance door )
        {
            buildPacket( door );
        }

        private void buildPacket( L1DoorInstance door )
        {
            writeC( Opcodes.S_OPCODE_CHARPACK );
            writeH( door.getX() );
            writeH( door.getY() );
            writeD( door.getId() );
            writeH( door.getGfxId() );
            int doorStatus = door.getStatus();
            int openStatus = door.getOpenStatus();
            if ( door.isDead() )
            {
                writeC( doorStatus );
            }
            else if ( openStatus == ActionCodes.ACTION_Open )
            {
                writeC( openStatus );
            }
            else if ( door.getMaxHp() > 1 && doorStatus != 0 )
            {
                writeC( doorStatus );
            }
            else
            {
                writeC( openStatus );
            }
            writeC( 0 );
            writeC( 0 );
            writeC( 0 );
            writeD( 1 );
            writeH( 0 );
            writeS( null );
            writeS( null );
            int status = 0;
            if ( door.getPoison() != null )
            { // 毒状態
                if ( door.getPoison().getEffectId() == 1 )
                {
                    status |= STATUS_POISON;
                }
            }
            writeC( status );
            writeD( 0 );
            writeS( null );
            writeS( null );
            writeC( 0 );
            writeC( 0xFF );
            writeC( 0 );
            writeC( 0 );
            writeC( 0 );
            writeC( 0xFF );
            writeC( 0xFF );
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = _bao.ToArray();
            }

            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_DoorPack";
        }
    }
}
